var CDN_PREFIX = 'https://zpdemo.xilukeji.com';

//
// 将 Date 转化为指定格式的String, date可以是int时间戳或 Date对象
// 月(m)、日(d)、小时(h)、分(i)、秒(s)、季度(q) 可以用 1-2 个占位符，
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子：
// formatDatetime(date, "yyyy-mm-dd hh:ii:ss.S") ==> 2006-07-02 08:09:04.423
// formatDatetime(date, "yyyy-m-d h:i:s.S")      ==> 2006-7-2 8:9:4.18
var formatDatetime = function(date, fmt) { //author: meizz
  if(date==='now') {
    date = getDate();
  }
  if(!date) {
    return "";
  }
  if(date.constructor !== 'Date') {
    if (getRegExp('^\d+$').test(date)) {
      date = (date + "").length > 10 ? date : date * 1000;
      date = getDate(parseInt(date));
    } else {
      date = getDate(date.replace(getRegExp('-', 'g'), '/'));
    }
  }

  var o = {
    "m+": date.getMonth() + 1, //月份
    "d+": date.getDate(), //日
    "h+": date.getHours(), //小时
    "i+": date.getMinutes(), //分
    "s+": date.getSeconds(), //秒
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
    "S": date.getMilliseconds() //毫秒
  };
  var yearMatch = fmt.match(getRegExp('(y+)'));
  if (yearMatch) {
    fmt = fmt.replace(yearMatch[0], (date.getFullYear() + "").substring(4 - yearMatch[0].length));
  }
  ["m+","d+","h+","i+","s+","q+","S"].forEach(function(k){
    var match = fmt.match(getRegExp("(" + k + ")"));
    if (match)
      fmt = fmt.replace(match[0], (match[0].length == 1) ? (o[k]) : (("00" + o[k]).substring(("" + o[k]).length)));
  });
  return fmt;
};

// 格式化时间为 "08:45"(今日)  "昨天 08:45" "前天 08:45" "2019-09-09 12:12"
var beautyTime = function (date) {
  if(!date) {
    return "";
  }
  if(date.constructor !== 'Date') {
    if (getRegExp('^\d+$').test(date)) {
      date = (date + "").length > 10 ? date : date * 1000;
      date = getDate(parseInt(date));
    } else {
      date = getDate(date.replace(getRegExp('-', 'g'), '/'));
    }
  }
  var now = getDate();
  var dateStr = formatDatetime(date,'yyyy-mm-dd');
  if(dateStr===formatDatetime(now,'yyyy-mm-dd')) {
    return formatDatetime(date, 'hh:ii');
  }
  if(dateStr===formatDatetime(now.getTime()-86400000,'yyyy-mm-dd')) {
    return formatDatetime(date, '昨天 hh:ii');
  }
  if(dateStr===formatDatetime(now.getTime()-86400000*2,'yyyy-mm-dd')) {
    return formatDatetime(date, '前天 hh:ii');
  }
  return formatDatetime(date, 'yyyy-mm-dd hh:ii:ss');
};

/**
 *
 * @param search
 * @param replace
 * @param str
 */
var strReplace = function (search, replace, str) {
  if(!str)
    return '';
  search = getRegExp(search,'g');
  return str.replace(search, replace);
};

var strSplit = function(delimiter, str, limit=undefined) {
  if(!str)
    return [];
  return str.split(delimiter, limit);
};

var strIndex = function (str, findStr) {
  return str.indexOf(findStr);
};

/**
 *
 */
var inArray = function(value, array) {
  return array ? array.indexOf(value)>=0 : false;
};
var getListItemById = function(list, id) {
  if(!list || !id)
    return null;
  for(var i=0;i<list.length;i++) {
    if(list[i].id==id) {
      return list[i];
    }
  }
  return null;
},
getListItemAttrById = function(list, id, attrName, defaultValue='') {
  var item = getListItemById(list, id);
  if(item && item[attrName]) {
    return item[attrName];
  }
  return defaultValue;
};
max = function (val, max) {
  return val > max ? max : val;
};
module.exports = {
  formatDatetime: formatDatetime,
  beautyTime: beautyTime,
  strReplace: strReplace,
  split: strSplit,
  inArray: inArray,
  strIndex: strIndex,
  cdnurl: function (path) {
    return getRegExp('^http').test(path) || !path ? path : CDN_PREFIX + (getRegExp('^/').test(path)?path:'/'+path);
  },
  // 从object数组中取选定的object ,判断条件 object.id==id
  getListItemById: getListItemById,
  // 从object数组中取选定的object的某个属性 ,判断条件 object.id==id
  getListItemAttrById: getListItemAttrById,
  max: max,

};
